package de.jmda.sample.ramcomp;

import java.io.IOException;

import de.jmda.core.util.ramcomp.RAMCompiler;
import de.jmda.core.util.ramcomp.RAMCompiler.RAMCompilerData;

public class DefineCompileAndRunJavaSourceCodeInRAM
{
	public static void main(String[] args)
			throws InstantiationException,
			       IllegalAccessException,
			       ClassNotFoundException,
			       IOException
  {
	  RAMCompilerData data1 =
	  		new RAMCompilerData(
	  				"C1",
	  				"public class C1 implements Runnable            \n" +
	  				"{                                              \n" +
	  				"  public void run()                            \n" +
	  				"  { System.out.println(getClass().getName()); }\n" +
	  				"}");
	  RAMCompilerData data2 =
	  		new RAMCompilerData(
	  				"C2",
	  				"public class C2 implements Runnable            \n" +
	  				"{                                              \n" +
	  				"  public void run()                            \n" +
	  				"  { System.out.println(getClass().getName()); }\n" +
	  				"}");

	  RAMCompiler.compile(data1, data2);

	  Object object;

	  // run first class
		object =
				data1.getClassLoader()
				     .loadClass(data1.getQualifiedTypeName()).newInstance();
		((Runnable) object).run();

	  // run second class
		object =
				data2.getClassLoader()
				     .loadClass(data2.getQualifiedTypeName()).newInstance();
		((Runnable) object).run();
  }
}